package main;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length != 1) {
			System.out.println("1 arg is needed");
			System.exit(0);
		}
		String path = args[0];
		File f = new File(path);
		if (!f.canRead())
			try {
				throw new FileNotFoundException("Can't read file " + f.getAbsolutePath());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				System.exit(0);
			}
		int blockSize = 1024;
		int blockCount = 51200/blockSize;
		File[] files = f.listFiles();
		for(File f1 : files) {
			if(f1.getName().equals("config")) {
				if(f1.canRead()) {
					FileInputStream fin = null;
					try {
						fin = new FileInputStream(f);
						DataInputStream in = new DataInputStream(fin);
					    BufferedReader br = new BufferedReader(new InputStreamReader(in));
					    String str1 = br.readLine();
					    String[] strs1 = str1.split("=");
					    blockSize = Integer.valueOf(strs1[1].trim());
					    String str2 = br.readLine();
					    String[] strs2 = str2.split("=");
					    blockCount = Integer.valueOf(strs2[1].trim());
					    fin.close();
					    in.close();
					    br.close();
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		System.out.println("blocksize " + blockSize + " blockCount " + blockCount);
		System.out.println(path + "filev");
		byte[] bs = new byte[blockSize];
		//Инициализируем нулями все файлы созданной ФС
		for(int i =0; i <blockSize - 1; i++) {
			bs[i] = 0;
		}
		try {
			for(int i = 0; i < blockCount; i++) {
				File p = new File(path + "/file" + i);
				FileOutputStream fout = new FileOutputStream(p);
				fout.write(bs);
				fout.close();
				System.out.println(p.getName() +" "+ p.length());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
